<?php
if (!defined('ABSPATH')) {
	define( 'ABSPATH', dirname(dirname(__FILE__)) . '/' );
}

require_once ABSPATH . 'dr-config.php';
require_once ABSPATH . DRINC . '/dr-base.php';
require_once ABSPATH . DRINC . '/dr-functions.php';
require_once ABSPATH . DRINC . '/dr-fun-users.php';
require_once ABSPATH . DRINC . '/dr-themes.php';
require_once ABSPATH . DRINC . '/dr-smarty.php';

require_once ABSPATH . DRINC . '/dr-page-check-login.php';

require_once ABSPATH . DRINC . '/mygravatar.php';
# 确认session开启
if (!isset($_SESSION)) {
	session_start();
}

# 如果用户未登陆，则跳转到登陆页面
if (!($user_id = dr_is_login_on())) {
	$redirect=BASE_HOSTPATH . "dr-includes/dr-page-login.php";
	header("Location: $redirect");
	exit;
}

$us_tab = isset($_GET['tab']) ? trim($_GET['tab']) : 'profile';
$white_tabs = array('profile', 'gravatar');
$tab = in_array($us_tab, $white_tabs) ? $us_tab : $white_tabs[0];

$profile_items = array(
	'username', 'nickname', 'realname', 'email',
	'gender', 'birthday', 'website', 'location', 'aboutme'
);

if (dr_post('submit')) {
	$profile = array();
	foreach ($profile_items as $item) {
		$profile[$item] = dr_post($item);
	}
	# 过滤用户的输入
	# XXX 过滤用户的输入个人信息
	
	dr_update_user_profile_after_edit($user_id, $profile);
	header("Location: dr-page-userinfo.php?uid=$user_id");
}

# 获得用户待编辑的个人信息
$profile = dr_get_user_profile_for_edit($user_id);

$smarty->assign('page_title', $profile['username'] . ' - 信息更改');

$avatar = (string) new MyGravatar($profile['email'], 128);
$smarty->assign('avatar', $avatar);
$smarty->assign('user', $profile);


$content = array();
$content['top_navs'] = get_page_top_navigation();
$content['menus'] = get_page_menus('users');
$content['tabs'] = get_page_tabs('user_profile_edit', $tab);
$content['current_tab'] = $tab;
$content['tabs']['title'] .= ' - ' . $profile['username'];
$smarty->assign('content', $content);



$smarty->display('admin/edit_info.tpl');

# =================== #
function varify_profile_info($profile, $profile_items) {
	# 过滤用户的输入
	# 返回结果
	return true;
}
